<?php
/**
 * 企业产品 控制器
 * @author ThankPHP
 * @version 0.1
 * @create time 2009-4-6 ThankPHP
 * @update time 2009-4-6 ThankPHP
 */
class EproductsAction extends Action {
	/**
	 * 管理所有的产品，并分页显示
	 * @create time 2009-4-6 ThankPHP
	 * @update time 2009-4-6 ThankPHP
	 */
	public function AdminIndex(){
		EasyACL('Eproducts','AdminIndex',$_SESSION);
		//实例化
		$Products = D('Eproducts');
		//计算总产品数量
		$count = $Products->count('','pid');
		//每页显示数量
		$listRows = 15;
		//显示字段
		$fields = 'pid,enter_id,name,created';
		//引入分页类
		import('ORG.Util.Page');
		$p = new Page($count,$listRows);
		$list = $Products->xfindall(null,$fields,'pid desc',$p->firstRow.','.$p->listRows);
		$page = $p->show();

		//置入到模板
		$this->assign('Products',$list);
		$this->assign('page',$page);
		$this->display();
	}
	/**
	 * 辅助企业创建产品
	 * * @create time 2009-4-6 ThankPHP
	 * * @update time 2009-4-6 ThankPHP
	 */
	public function AdminCreate(){
		EasyACL('Eproducts','AdminCreate',$_SESSION);
		if ($this->isPost()) {
			$Error = ''; //用来保存错误信息
			//上传文件
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			//设置上传文件大小
			$upload->maxSize  = 266240;
			//设置上传文件类型
			$upload->allowExts  = explode(',','jpg,gif,png');
			//设置附件上传目录
			$upload->savePath =  './upload/';

			//执行上传操作
			if(!$upload->upload()) {
				$Error .= '图片上传失败!';
			}else{
				$AllFiles = $upload->getSuccessFiles();
			}

			//设置输入类
			$Input = Input::getInstance();
			//$Input->filter('addslashes');
			//获取企业分类 设置查询条件
			import('ORG.Util.HashMap');
			$conditions = new HashMap();
			$conditions->put('enter_id',array('EQ',$Input->post('enter_id')));
			$Enterprise = D('enterprise');
			$EInfo = $Enterprise->where($conditions)->field('cid')->find();

			//保存到数据库中
			$Products = D('Eproducts');
			$Products->enter_id = (int)$Input->post('enter_id');
			$Products->cid = (int)$EInfo['cid'];
			$Products->name = htmlspecialchars($Input->post('name'));
			$Products->bimg = './upload/'.$AllFiles[0]['savename'];
			$Products->videocode = $Input->post('videocode');
			$Products->content = $Input->post('content');
			$Products->created = time();
			$result = $Products->add();
			if (!$result){
				return $this->error($Products->getError());
				//halt('出错!尝试存入数据库失败~');
			}
			return $this->redirect('AdminIndex','Eproducts');

		}
		return $this->display();
	}

	/**
	 * 修改产品信息
	 * * @create time 2009-4-6 ThankPHP
	 * * @update time 2009-4-6 ThankPHP
	 */
	public function AdminUpdate(){
		EasyACL('Eproducts','AdminUpdate',$_SESSION);
		
		$Input = Input::getInstance();
		//$Input->filter('addslashes');
		
		$Product = D('Eproducts');
		
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
        //POST提交
		if($this->isPOST()){
			$Error = ''; //用来保存错误信息
			if (!empty($_FILES['file']['name'])) {
				@unlink($Input->post('Proimgadd'));
				//上传文件
				import("ORG.Net.UploadFile");
				$upload = new UploadFile();
				//设置上传文件大小
				$upload->maxSize  = 266240;
				//设置上传文件类型
				$upload->allowExts  = explode(',','jpg,gif,png');
				//设置附件上传目录
				$upload->savePath =  './upload/';
	
				//执行上传操作
				if(!$upload->upload()) {
					$Error .= '图片上传失败!';
				}else{
					$AllFiles = $upload->getSuccessFiles();
				}
			}
			//查询要修改的信息
			$conditions->put('pid',array('EQ',$Input->post('pid')));
			$Product->where($conditions)->find();
			$Product->enter_id = $Input->post('enter_id');
			$Product->name = $Input->post('name');
			//如果没有上传图片
			if (!empty($AllFiles)){
				$Product->bimg = './upload/'.$AllFiles[0]['savename'];
			}
			$Product->videocode = $Input->post('videocode');
			$Product->content = $Input->post('content');
			$result = $Product->save();
			if (!$result){
				halt('尝试修改产品信息失败!');
			}
			return $this->redirect('AdminIndex','Eproducts');
		}
        //初始页面
        //获取要修改的产品信息
		$Pid = (int)$Input->get('pid');
		//设定查询条件
		$conditions->put('pid',array('EQ',$Pid));
		$ProInfo = $Product->where($conditions)->find(); //获取要修改的产品信息
		
		//设置到模板
		$this->assign('ProInfo',$ProInfo);
        return $this->display();
	}
	
	/**
	 * 删除单个产品信息
	 * 删除单个产品或多个产品都是将值传入到Dels动作，Dels动作会根据传入值进行操作
	 * @create time 2009-4-6 ThankPHP 
	 * @update time 2009-4-6 ThankPHP
	 * 
	 * @param $val int
	 */
	public function AdminDel($val){
		EasyACL('Eproducts','AdminDel',$_SESSION);
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('pid',array('EQ',(int)$val));
		//删除操作
		$Product = D('Eproducts');
		$ProductInfo = $Product->field('pid,bimg')->where($conditions)->find();
		@unlink($ProductInfo['bimg']);
		$result = $Product->deleteById($val);
		if (!$result) {
			return FALSE;
		}
		return TRUE;
	}
	
	/**
	 * 根据传入值，判断删除多个或者单个
	 */
	public function AdminDels(){
		EasyACL('Eproducts','AdminDels',$_SESSION);
		//如果是GET传入
		if ($this->isGet()){
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			$result = $this->AdminDel($Input->get('pid'));
			if ($result == FALSE) {
				halt('尝试删除产品失败!');
			}
			return $this->redirect('AdminIndex','Eproducts');
		}
		//如果是POST传入
		if ($this->isPost()){
			$Pids = $_POST['pid'];
			$Error = '';
			foreach ($Pids as $pid){
				$result = $this->AdminDel($pid);
				if (!$result){
					$Error .= '尝试删除编号为'.$pid.'产品失败<br />';
				}
			}
			if (!empty($Error)) {
				halt($Error);
			}
			return $this->redirect('AdminIndex','Eproducts');
		}
		return $this->redirect('AdminIndex','Eproducts');
	}
}










